/*
 */
package beans;

import db_connect.DB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Milan Boricic <boricic@gmail.com>
 */
public class Sala {

    private Integer sid;
    private String naziv;
    private String lokacija;
    private Integer kapacitet;
    private Integer brRacunara;
    private boolean imaTablu;
    private boolean imaProjektor;
    

    public Integer getBrRacunara() {
        return brRacunara;
    }

    public void setBrRacunara(Integer BrRacunara) {
        this.brRacunara = BrRacunara;
    }

    public boolean isImaProjektor() {
        return imaProjektor;
    }

    public void setImaProjektor(boolean ImaProjektor) {
        this.imaProjektor = ImaProjektor;
    }

    public boolean isImaTablu() {
        return imaTablu;
    }

    public void setImaTablu(boolean ImaTablu) {
        this.imaTablu = ImaTablu;
    }

    public Integer getKapacitet() {
        return kapacitet;
    }

    public void setKapacitet(Integer Kapacitet) {
        this.kapacitet = Kapacitet;
    }

    public String getLokacija() {
        return lokacija;
    }

    public void setLokacija(String Lokacija) {
        this.lokacija = Lokacija;
    }

    public String getNaziv() {
        return naziv;
    }

    public void setNaziv(String naziv) {
        this.naziv = naziv;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    /** Creates a new instance of Sala */
    public Sala() {
    }

    public ArrayList getSaleSve() {
        ArrayList<Sala> sale = new ArrayList<Sala>();

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            String query = "select * from sala";
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                Sala sala = new Sala();

                sala.setSid(rs.getInt("sid"));
                sala.setNaziv(rs.getString("naziv"));
                sala.setLokacija(rs.getString("Lokacija"));
                sala.setKapacitet(rs.getInt("Kapacitet"));
                sala.setBrRacunara(rs.getInt("BrRacunara"));
                sala.setImaTablu(rs.getBoolean("ImaTablu"));
                sala.setImaProjektor(rs.getBoolean("ImaProjektor"));

                sale.add(sala);
            }

            stmt.close();
            con.close();

        } catch (SQLException sqle) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }

        return sale;
    }


    public String informacije() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik korisnik = (Korisnik) session.getAttribute("korisnik");

        korisnik.setIzabrana(this);

        korisnik.setOutcome("sala");
        return korisnik.isIsAdmin() ? Korisnik.ACTION_ADMIN : Korisnik.ACTION_KORISNIK;

    }

    public String raspored() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik korisnik = (Korisnik) session.getAttribute("korisnik");

        korisnik.setIzabrana(this);
        Date datum = new Date(System.currentTimeMillis());
        Calendar newCal = new GregorianCalendar();
        newCal.setTime(datum);

        int day = newCal.get(Calendar.DAY_OF_WEEK);
        int fromMonday = day - Calendar.MONDAY;

        Calendar monday = new GregorianCalendar();
        monday.setTime(datum);
        monday.set(Calendar.DAY_OF_MONTH, newCal.get(Calendar.DAY_OF_MONTH) - fromMonday);
        System.out.print(monday.getTime());

        korisnik.rasporedRefresh();

        korisnik.setOutcome("raspored");
        return korisnik.isIsAdmin() ? Korisnik.ACTION_ADMIN : Korisnik.ACTION_KORISNIK;

    }

    public String viewEditSala() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik korisnik = (Korisnik) session.getAttribute("korisnik");

        korisnik.setIzabrana(this);

        korisnik.setOutcome("edit_sala");
        return Korisnik.ACTION_ADMIN;
    }

    public String edit() {
        Connection con = null;
        Statement stmt = null;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik korisnik = (Korisnik) session.getAttribute("korisnik");
        
        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            String query = "update sala set " +
                    "naziv = '" + naziv + "', " +
                    "Lokacija = '" + lokacija + "', " +
                    "Kapacitet = " + kapacitet + ", " +
                    "BrRacunara = " + brRacunara + ", " +
                    "ImaTablu = " + imaTablu + ", " +
                    "ImaProjektor = " + imaProjektor + " " +
                    "where sid = " + sid + ";";


            int res = stmt.executeUpdate(query);

            if(res == 1) {
                context.addMessage(null, new FacesMessage("Informacije o sali uspešno izmenjene"));
            }

            stmt.close();
            con.close();

        } catch (SQLException sqle) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }

        return korisnik.isIsAdmin() ? Korisnik.ACTION_ADMIN : Korisnik.ACTION_KORISNIK;
    }
}
